package com.taiji.workOrder.service.impl;

import java.util.Date;
import java.util.List;

import com.taiji.common.core.domain.entity.SysUser;
import com.taiji.common.utils.DateUtils;
import com.taiji.common.utils.SecurityUtils;
import com.taiji.system.mapper.SysUserMapper;
import com.taiji.workOrder.Utils.Const;
import com.taiji.workOrder.domain.OrderEnclosure;
import com.taiji.workOrder.domain.OrderLogs;
import com.taiji.workOrder.domain.WorkOrder;
import com.taiji.workOrder.mapper.MyOrderMapper;
import com.taiji.workOrder.mapper.OrderEnclosureMapper;
import com.taiji.workOrder.mapper.OrderLogsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taiji.workOrder.mapper.MyDealMapper;
import com.taiji.workOrder.service.IMyDealService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 我处理的Service业务层处理
 * 
 * @author taiji
 * @date 2023-03-19
 */
@Service
public class MyDealServiceImpl implements IMyDealService 
{

    @Autowired
    private MyOrderMapper myOrderMapper;
    @Autowired
    private MyDealMapper myDealMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrderLogsMapper orderLogsMapper;

    @Autowired
    private OrderEnclosureMapper enclosureMapper;

    /**
     * 查询我处理的
     * 
     * @param id 我处理的主键
     * @return 我处理的
     */
    @Override
    public WorkOrder selectMyDealById(String id)
    {
        // 根据id拿到工单
        WorkOrder workOrder = myOrderMapper.selectMyOrderById(id);
        OrderEnclosure orderEnclosure = new OrderEnclosure();
        // 新建工单附件对象 设置对象的 工单编号
        orderEnclosure.setWorkNumber(workOrder.getWorkNumber());
        List<OrderEnclosure> orderEnclosures = enclosureMapper.selectOrderEnclosureList(orderEnclosure);
        // 获取工单附件List
        workOrder.setOrderEnclosureList(orderEnclosures);
        OrderLogs orderLogs = new OrderLogs();
        orderLogs.setWorkNumber(workOrder.getWorkNumber());
        //获取流转日志 根据处理时间先后排序 返回前端
        List<OrderLogs> orderLogsList = orderLogsMapper.selectOrderLogsList(orderLogs);

        workOrder.setOrderLogs(orderLogsList);
        return workOrder;

    }

    /**
     * 查询我处理的列表
     * 
     * @param myDeal 我处理的
     * @return 我处理的
     */
    @Override
    public List<WorkOrder> selectMyDealList(WorkOrder myDeal)
    {
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        myDeal.setDealPerson(userName);
        return myDealMapper.selectMyDealList(myDeal);
    }

    /**
     * 新增我处理的
     * 
     * @param myDeal 我处理的
     * @return 结果
     */
    @Override
    public int insertMyDeal(WorkOrder myDeal)
    {
        myDeal.setCreateTime(DateUtils.getNowDate());
        return myDealMapper.insertMyDeal(myDeal);
    }

    /**
     * 修改我处理的
     * 
     * @param myDeal 我处理的
     * @return 结果
     */
    @Override
    public int updateMyDeal(WorkOrder myDeal)
    {
        myDeal.setUpdateTime(DateUtils.getNowDate());
        return myDealMapper.updateMyDeal(myDeal);
    }

    /**
     * 批量删除我处理的
     * 
     * @param ids 需要删除的我处理的主键
     * @return 结果
     */
    @Override
    public int deleteMyDealByIds(String[] ids)
    {
        return myDealMapper.deleteMyDealByIds(ids);
    }

    /**
     * 删除我处理的信息
     * 
     * @param id 我处理的主键
     * @return 结果
     */
    @Override
    public int deleteMyDealById(String id)
    {
        return myDealMapper.deleteMyDealById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int revert(WorkOrder workOrder)
    {
        Date date = new Date();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        //日志
        String dealPerson = workOrder.getDealPerson();
        SysUser sysUser = sysUserMapper.selectUserByUserName(dealPerson);
        OrderLogs orderLogs = new OrderLogs();
        orderLogs.setCreateBy(nickName);
        orderLogs.setCreateTime(date);
        orderLogs.setContents(nickName+"转单给"+sysUser.getNickName());
        orderLogs.setWorkNumber(workOrder.getWorkNumber());
        orderLogs.setRemark(workOrder.getRemark());
        orderLogsMapper.insertOrderLogs(orderLogs);
        workOrder.setUpdateTime(date);
        workOrder.setUpdateBy(nickName);
        workOrder.setRemark(null);
        return myDealMapper.updateMyDeal(workOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int finish(String workNumber)
    {
        Date date = new Date();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        //日志
        WorkOrder workOrder = myOrderMapper.selectMyOrderByWorkNumber(workNumber);
        OrderLogs orderLogs = new OrderLogs();
        orderLogs.setCreateBy(nickName);
        orderLogs.setCreateTime(date);
        orderLogs.setContents(nickName+"完成工单");
        orderLogs.setWorkNumber(workOrder.getWorkNumber());
        orderLogsMapper.insertOrderLogs(orderLogs);
        workOrder.setUpdateTime(date);
        workOrder.setUpdateBy(nickName);
        workOrder.setStatus(Const.WorkOrderStatus.FINISH.getKey());
        workOrder.setRemark(null);
        return myDealMapper.updateMyDeal(workOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int close(String workNumber)
    {
        Date date = new Date();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        //日志
        WorkOrder workOrder = myOrderMapper.selectMyOrderByWorkNumber(workNumber);
        OrderLogs orderLogs = new OrderLogs();
        orderLogs.setCreateBy(nickName);
        orderLogs.setCreateTime(date);
        orderLogs.setContents(nickName+"关闭工单");
        orderLogs.setWorkNumber(workOrder.getWorkNumber());
        orderLogsMapper.insertOrderLogs(orderLogs);
        workOrder.setUpdateTime(date);
        workOrder.setUpdateBy(nickName);
        workOrder.setStatus(Const.WorkOrderStatus.CLOSED.getKey());
        workOrder.setRemark(null);
        return myDealMapper.updateMyDeal(workOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int accept(String workNumber)
    {
        Date date = new Date();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        //日志
        WorkOrder workOrder = myOrderMapper.selectMyOrderByWorkNumber(workNumber);
        OrderLogs orderLogs = new OrderLogs();
        orderLogs.setCreateBy(nickName);
        orderLogs.setCreateTime(date);
        orderLogs.setContents(nickName+"开始受理工单");
        orderLogs.setWorkNumber(workOrder.getWorkNumber());
        orderLogsMapper.insertOrderLogs(orderLogs);
        workOrder.setUpdateTime(date);
        workOrder.setUpdateBy(nickName);
        workOrder.setDealPerson(SecurityUtils.getLoginUser().getUser().getUserName());
        workOrder.setStatus(Const.WorkOrderStatus.DOING.getKey());
        workOrder.setRemark(null);
        return myDealMapper.updateMyDeal(workOrder);
    }

    @Override
    public WorkOrder selectWorkOrderByWorkNumber(String workNumber) {


        return myDealMapper.selectWorkOrderByWorkNumber(workNumber);
    }

}
